package com.xywy.me;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.FragmentMainActivity;
import com.xywy.common.constants.ConstantValue;
import com.xywy.common.constants.ResultCode;
import com.xywy.common.net.AsyncHttpClient;
import com.xywy.common.net.OnResultListenerAdapter;
import com.xywy.common.net.Parser;
import com.xywy.common.util.MLog;
import com.xywy.common.widget.MyProgress;
import com.xywy.common.widget.TitleBar2;
import com.xywy.me.bean.UserKeeper;

import java.util.HashMap;

/**
 * 我 - 设置 - 账户 - 新旧密码
 * 
 * @author xiaogd
 * 
 */
public class SetupAcPwdActivity extends BaseActivity {
	private static final String TAG = "SetupAcPwdActivity";
	private String target = "";
	private String userid = "";

	private Context mContext;
	private EditText pwd;
	private EditText pwd2;
	private ImageView pCancel;
	private ImageView p1Cancel;
	private Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置为全屏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.me_setup_ac_pwd);

		this.preInit();
		this.initView();
		this.loadData();
		this.setListener();
	}

	@Override
	public void preInit() {
		mContext = this.context;
		Intent intent = this.getIntent();
		target = intent.getStringExtra("target");
//		userid = intent.getStringExtra("userid");// userid
		userid = UserKeeper.getCurrentUser(this).getUserid();

	}

	@Override
	public void initView() {
		// title
		TitleBar2 titleBar = (TitleBar2) findViewById(R.id.me_setup_pwd_title_bar);
		titleBar.onClickBack(this);
		TextView tvTitle = titleBar.getTitle();
		TextView tvBack = titleBar.getTvBack();
		titleBar.getGo().setVisibility(View.INVISIBLE);
		if (null != target && target.equals(ConstantValue.LOGINVALCODE)) {
			tvBack.setVisibility(View.INVISIBLE);
			// titleBar.getBtnBack().setVisibility(View.INVISIBLE);
		}
		tvTitle.setText(getString(R.string.me_setup_account_szxmm));
		tvBack.setText(getString(R.string.me_setup_account));

		pwd = (EditText) findViewById(R.id.et_me_setup_pwd);
		pwd2 = (EditText) findViewById(R.id.et_me_setup_pwd_2);
		pCancel = (ImageView) findViewById(R.id.iv_start_setpwd_pcancel);
		p1Cancel = (ImageView) findViewById(R.id.iv_start_setpwd_p1cancel);

		button = (Button) findViewById(R.id.tv_setup_ac_ghsj);
	}

	public void startOnClick(View view) {
		switch (view.getId()) {
		case R.id.tv_setup_ac_ghsj:
			next();
			break;
		case R.id.iv_start_setpwd_pcancel:
			pwd.setText("");
			break;
		case R.id.iv_start_setpwd_p1cancel:
			pwd2.setText("");
			break;

		}
		;
	}

	private void next() {

		// 进行验证,发送网络请求
		boolean flag = false;
		if (pwd.getText().toString() == null
				|| pwd.getText().toString().length() == 0) {
			Toast.makeText(mContext, getString(R.string.me_pwd_not_null),
					Toast.LENGTH_LONG).show();
			flag = true;
		} else if (pwd2.getText().toString() == null
				|| pwd2.getText().toString().length() == 0) {
			Toast.makeText(mContext, getString(R.string.me_pwd_not_null),
					Toast.LENGTH_LONG).show();
			flag = true;
		} else if (pwd.getText().toString().length() < 6
				|| pwd.getText().toString().length() > 16) {
			Toast.makeText(mContext, getString(R.string.me_pwd_change_prompt),
					Toast.LENGTH_LONG).show();
			flag = true;
		} else if (!pwd.getText().toString().equals(pwd2.getText().toString())) {
			Toast.makeText(mContext, getString(R.string.me_pwd_not_same),
					Toast.LENGTH_LONG).show();
			flag = true;
		}
		if (!flag) {
			this.sendMsg();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	protected void onPause() {
		super.onPause();

	}

	@Override
	public void loadData() {

	}

	private void setListener() {
		pwd.addTextChangedListener(pTextWatcher);
		pwd2.addTextChangedListener(p2TextWatcher);
	}

	boolean mFlag = false;
	TextWatcher pTextWatcher = new TextWatcher() {
		@Override
		public void beforeTextChanged(CharSequence charSequence, int i, int i2,
				int i3) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}

		@Override
		public void afterTextChanged(Editable editable) {
			if (editable.length() > 0) {
				pCancel.setVisibility(View.VISIBLE);
				mFlag = true;
				if (nFlag) {
					button.setBackgroundDrawable(mContext.getResources()
							.getDrawable(R.drawable.shape_button));
					button.setClickable(true);
				}
			} else {
				pCancel.setVisibility(View.INVISIBLE);
				mFlag = false;
				button.setBackgroundDrawable(mContext.getResources()
						.getDrawable(R.drawable.shape_button_bgwhite));
				button.setClickable(false);
			}
		}
	};

	boolean nFlag = false;
	TextWatcher p2TextWatcher = new TextWatcher() {
		@Override
		public void beforeTextChanged(CharSequence charSequence, int i, int i2,
				int i3) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}

		@Override
		public void afterTextChanged(Editable editable) {
			if (editable.length() > 0) {
				p1Cancel.setVisibility(View.VISIBLE);
				nFlag = true;
				if (mFlag) {
					button.setBackgroundDrawable(mContext.getResources()
							.getDrawable(R.drawable.shape_button));
					button.setClickable(true);
				}
			} else {
				p1Cancel.setVisibility(View.INVISIBLE);
				nFlag = false;
				button.setBackgroundDrawable(mContext.getResources()
						.getDrawable(R.drawable.shape_button_bgwhite));
				button.setClickable(false);
			}
		}
	};

	MyProgress myProgress;
	/**
	 * 返回
	 */
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			MLog.i(TAG, "back:" + msg.obj + "resuletCode:" + msg.arg1);
			myProgress.stop();
			if (msg.arg1 == ResultCode.SUCCESS) {
				Toast.makeText(mContext,
						getString(R.string.me_updatepwd_success),
						Toast.LENGTH_LONG).show();

				// if (null != target &&
				// target.equals(ConstantValue.LOGINVALCODE)) {
				Intent intent = new Intent(mContext, FragmentMainActivity.class);
				startActivity(intent);
				// finish();
				// }
				SetupAcPwdActivity.this.finish();
			} else {
				Toast.makeText(mContext, getString(R.string.me_updatepwd_fail),
						Toast.LENGTH_LONG).show();
			}
		}
	};

	// 封装Parser参数
	private Parser paramsParser;
	// 封装httpclient参数
	private HashMap<String, Object> params;
	// 异步加载
	private AsyncHttpClient asyncHttpClient;

	// 处理网络请求，修改密碼
	public void sendMsg() {
		myProgress = new MyProgress(this, 10);
		myProgress.start();
		paramsParser = new Parser();

		// 封装参数
		params = new HashMap<String, Object>();

		// 请求路径
		paramsParser.action = getString(R.string.update_user_infor);
		// 请求方式
		paramsParser.request = "post";

		// if (userid.equals("")) {
		// SharedPreferences sp = context.getSharedPreferences("config",
		// Context.MODE_PRIVATE);
		// userid = sp.getString("userid", "");
		// }

		// 请求参数
		params.put("userid", userid);
		params.put("keyword", ConstantValue.PASSWORD);
		params.put("value", pwd.getText().toString());
		paramsParser.map = params;
		// 传递上下文
		paramsParser.context = this.context;

		asyncHttpClient = AsyncHttpClient.getInstance();
		asyncHttpClient.exeute(paramsParser, onResultListenerAdapter);
	}

	OnResultListenerAdapter onResultListenerAdapter = new OnResultListenerAdapter() {
		@Override
		public <T extends Parser> void onSuccess(T resultParser, int resultCode) {
			super.onSuccess(resultParser, resultCode);
			Message message = Message.obtain();
			message.arg1 = resultCode;
			message.obj = resultParser.back;
			handler.sendMessage(message);
		}
	};

}
